﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Fluent;
using System.Configuration;
using System.ComponentModel;
using devtm.Editor.Configuration;
using System.Collections.ObjectModel;
using devtm.Editor.Configuration.Messages;
using devtm.Editor.Configuration.ConfigurationProperties;

namespace ConfigEditor
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : RibbonWindow, INotifyPropertyChanged
    {


        devtm.Applications.ApplicationStep Appli;

        public MainWindow()
        {

            
            InitializeComponent();
            
            //Uri iconUri = new Uri(@"/ConfigEditor;component/Images/Editor.ico", UriKind.RelativeOrAbsolute);

            //this.Icon = BitmapFrame.Create(iconUri);



            Uc.TextInStream += Uc_TextInStream;
            if (!Uc.Init(Environment.GetCommandLineArgs()))
            {

            }

            Uc.PropertyChanged += Uc_PropertyChanged;

            /*
#if DEBUG
            // appelle le service de licence en directe.
            devtm.Applications.ApplicationStep.Client = new devtm.Applications.Implementation.Server();
#endif

            devtm.Applications.ApplicationStep.FirstStart += ApplicationStep_FirstStart;
            Appli = devtm.Applications.ApplicationStep.Instance;
            Appli.Initialize();
            */

            InitializeCommand();
            ContainerStatutList.Visibility = System.Windows.Visibility.Collapsed;


        }


        void ApplicationStep_FirstStart(object sender, devtm.Applications.FirstStartEventArgs e)
        {

        }


        void Uc_TextInStream(object sender, OutputBaseEventHandlerEventArgs e)
        {

            if (e.Action== System.Collections.Specialized.NotifyCollectionChangedAction.Add)
                StatutList.Items.Add(e.Message);

        }


        private void WindowsClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {

            try
            {
                Uc.Close();
                Uc.PropertyChanged -= Uc_PropertyChanged;
            }
            catch (Exception)
            {
                
            }

        }


        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            ((sender as System.Windows.Controls.Button).Tag as MessageText).Run();
        }


        private void Button_OpenRecentFile(object sender, RoutedEventArgs e)
        {

            StatutList.Items.Clear();

            RecentFile f = (sender as System.Windows.Controls.Button).Tag as RecentFile;

            try
            {

                Uc.Load(f.FullName);

                if (StatutList.Items.Count == 1)
                    ConfigurationRibbonTabItem.IsSelected = true;
                else
                    DiagnosticConfigurationRibbonTabItem.IsSelected = true;


            }
            catch (Exception)
            {
                
                throw;
            }
            

        }


        //private Visibility etat = Visibility.Visible;
        //private void ShowInherits(object sender, RoutedEventArgs e)
        //{

        //    etat = etat == Visibility.Collapsed
        //            ? System.Windows.Visibility.Visible
        //            : System.Windows.Visibility.Collapsed;

        //    if (etat == System.Windows.Visibility.Visible)
        //        ShowInherit.Header = "Hide";
        //    else
        //        ShowInherit.Header = "Show";

        //    Uc.ShowInherits(etat);
        //}


        private void RemoveDirectory(object sender, RoutedEventArgs e)
        {
            string key = ((sender as System.Windows.Controls.Button).Tag as string);
            Uc.ListeProperties.Directories.RemoveDirectory(key);
        }

    
        //public Visibility IsModified
        //{
        //    get
        //    {
        //        if (Uc.IsModified)
        //            return System.Windows.Visibility.Visible;

        //        return System.Windows.Visibility.Hidden;

        //    }
        //}

        #region Events change

        protected void PropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;

        void Uc_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
                PropertyChange(e.PropertyName);
        }

        #endregion

        private void CheckModeSave(object sender, RoutedEventArgs e)
        {

            if (Uc == null)
                return;

            if ((bool)radioSaveFull.IsChecked)
                Uc.SaveParameter.ConfigurationMode = ConfigurationSaveMode.Full;
            else
                if (radioSaveMinimum != null)
                {
                    if ((bool)radioSaveMinimum.IsChecked)
                        Uc.SaveParameter.ConfigurationMode = ConfigurationSaveMode.Minimal;
                    else
                        Uc.SaveParameter.ConfigurationMode = ConfigurationSaveMode.Modified;
                }
            Uc.SaveParameter.ForceSave = (bool)ForceSave.IsChecked;
            Uc.SaveParameter.Fusion = true;

        }

        private void UnCheckModeSave(object sender, RoutedEventArgs e)
        {

        }

        private void Ribbon_SelectedTabChanged(object sender, SelectionChangedEventArgs e)
        {

            if (e.RemovedItems.Count > 0)
            {

                foreach (var tab in e.RemovedItems)
                    SearchObject(tab, System.Windows.Visibility.Collapsed);

            }
            else
            {

                foreach (var tab in e.AddedItems)
                    SearchObject(tab, System.Windows.Visibility.Visible);
            
            }
            
        }

        private void SearchObject(object tab, System.Windows.Visibility visibility)
        {
            Fluent.RibbonTabItem t1 = tab as Fluent.RibbonTabItem;
            if (t1 != null)
                SelectObject(t1, visibility);
            else
            {
                throw new NotImplementedException(tab.GetType().Name);
            }
        }

        private void SelectObject(Fluent.RibbonTabItem tab, System.Windows.Visibility visibility)
        {
            if (Object.Equals(tab, ConfigurationRibbonTabItem))
                Uc.Visibility = visibility;

            else if (Object.Equals(tab, DiagnosticConfigurationRibbonTabItem))
            {
                ContainerStatutList.Visibility = visibility;
            }
            else
            {
                throw new NotImplementedException(tab.Name);
            }
        }


        //private void MergeFileConfig(object sender, RoutedEventArgs e)
        //{
        //    Uc.MergeCurrentSection();
        //}


  

    }
}
